<template>
  <div class="chapter-contents">
    <div class="chapter-list" v-if="chapterList.length > 0">
      <div
        class="chapter-list-item"
        :title="item.label"
        v-for="(item, index) in chapterList"
        :key="item.id"
        :class="{ active: selectItemId == item.id }"
        @click="handleClick(item.id)"
      >
        <span>第{{ index + 1 }}章 &nbsp;{{ item.label }}</span>
      </div>
    </div>
    <div
      class="chapter-chat"
      :style="{ width: chapterList.length > 0 ? '80%' : '100%' }"
    >
      <CommonChat_system
        v-if="selectItemId && chapterList.length > 0"
        :id="chapterId"
        ref="commonChatRef"
      />
      <div class="no-data" v-else>
        <img src="@/assets/img/no-data.png" alt="" />
        <div class="no-data-text">暂无内容</div>
      </div>
    </div>
  </div>
</template>

<script>
import CommonChat_system from "../../components/commonChat_system.vue";
export default {
  components: {
    CommonChat_system,
  },
  data() {
    return {
      selectItemId: 1,
      chapterList: [],
      courseId: JSON.parse(localStorage.getItem("courseId")),
      chapterId: null,
    };
  },
  created() {
    this.getChapterList();
  },
  methods: {
    // 获取章节
    getChapterList() {
      this.$axios
        .get(`/tes/course/auto/make/all-show-chapter?courseId=${this.courseId}`)
        .then((res) => {
          this.chapterList = res.data;
          if (this.chapterList.length > 0) {
            this.chapterId = Number(this.chapterList[0].id);
            this.selectItemId = Number(this.chapterList[0].id);
          }
        });
    },
    handleClick(id) {
      // 如果点击的是当前已选中的项目，不执行任何操作
      if (this.selectItemId === id) {
        return;
      }
      this.selectItemId = id;
      this.chapterId = Number(id);

      // 切换章节时重置右侧对话
      this.$nextTick(() => {
        if (this.$refs.commonChatRef) {
          this.$refs.commonChatRef.resetChat();
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.chapter-contents {
  overflow-x: hidden;
  display: flex;
  width: 100%;
  overflow: hidden;
  box-sizing: border-box;
  height: calc(100vh - 267px);
  .chapter-list {
    height: 100%;
    width: 20%;
    background-color: #fbfcff;
    padding: 30px 20px 44px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    &::-webkit-scrollbar {
      width: 4px;
    }

    &::-webkit-scrollbar-track {
      background: transparent;
      border-radius: calc(4px / 2);
    }

    &::-webkit-scrollbar-thumb {
      background: #fbfcff;
      border-radius: calc(4px / 2);
    }
    .chapter-list-item {
      width: 100%;
      font-size: 16px;
      color: #6b6a85;
      flex-shrink: 0;
      padding: 15px 30px;
      box-sizing: border-box;
      cursor: pointer;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      &.active {
        background: #e8efff;
        border-radius: 20px;
        color: #457aff;
      }
    }
  }
  .chapter-chat {
    width: 80%;
    .no-data {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding: 263px 0;
      gap: 10px;
      width: 100%;
      background-color: #f3f5fa;
      .no-data-text {
        font-size: 16px;
        color: #999;
      }
    }
  }
}
</style>
